import { defHttp } from '/@/utils/http/axios';
import { OptionFlowParams, FlowResult, FlowTemplateResult } from './model';
import { ResultEnum } from '/@/enums/httpEnum';
import { ErrorMessageMode } from '/#/axios';

enum Api {
  NewFlowPage = '/workflow/workItem/newWorkItem',
  GetFlowPage = '/workflow/workItem/openWorkItem',
  GetFlowXml = '/workflow/workItem/getBPMNXml',
  CreateFlowProcess = '/workflow/workItem/saveWorkItem',
  CreateFlowProcessReturn = '/workflow/workItem/saveReturnWorkItem',
  SendFlowProcess = '/workflow/workItem/sendWorkItem',
  SaveSendFlowProcess = '/workflow/workItem/saveSendWorkItem',
  CloseFlowProcess = '/workflow/workItem/releaseWorkItemLock',
  HighlightWorkItem = '/workflow/workItem/highlightWorkItem',
  ViewApprovalProcess = '/workflow/workItemInst/viewApprovalProcess',
  TemplateDefinePage = '/workflow/workItemInst/getTemplateDefineList',
  DeleteDraft = '/workflow/workItem/deleteDraft',
  TransferFlow = '/workflow/workItem/transferWorkItem',
  TransferIdeaFlow = '/workflow/workItem/updateTrnsferWorkItem',
  TurnToDoFlow = '/workflow/workItem/turnToDoWorkItem',
  DeleteTransferFlow = '/workflow/workItem/tombstoningTransfer',
  RestoreWorkItemInfo = '/workflow/workItem/getRestoreWorkItemInfo',
  RestoreNewEntryOptions = '/workflow/workItem/getRestoreNewEntryOptions',
  RestoreWorkItem = '/workflow/workItem/restoreWorkItem',
  RecallWorkItem = '/workflow/workItem/recallWorkItem',
  UrgingWorkItem = '/workflow/workItem/urgingWorkItem',
  WorkItemSignInfo = '/workflow/workItem/getWorkItemSignInfo',
  saveWorkItemSignInfo = '/workflow/workItem/saveWorkItemSignInfo',
  uploadForWorkItem = '/admin/sys-file/uploadForWorkItem',
  WatchNodeInfo = '/workflow/workItemInst/getLatestActivityApprovalInfo',
}

export const WorkItemSignInfoApi = (guid: string) =>
  defHttp.post<Indexable | null>({
    url: Api.WorkItemSignInfo,
    params: { guid },
  });

export const getNewFlowPageApi = (params: {
  cyclostyleID: number | string;
  moduleId: number | string;
  fieldsMap: { project_id: number | string };
}) =>
  defHttp.post<FlowResult>({
    url: Api.NewFlowPage,
    params,
  });

export const getFlowPageApi = (params?: OptionFlowParams) =>
  defHttp.post<FlowResult>(
    {
      url: Api.GetFlowPage,
      params,
    },
    {
      successCustomMsg: true,
      successMessageMode: 'msg',
    },
  );

export const createFlowProcessApi = (params) =>
  defHttp.post({
    url: Api.CreateFlowProcess,
    params,
  });

export const createFlowProcessReturnApi = (params) =>
  defHttp.post({
    url: Api.CreateFlowProcessReturn,
    params,
  });

export const sendFlowProcessApi = (params) =>
  defHttp.post({
    url: Api.SendFlowProcess,
    params,
  });

export const closeFlowProcessApi = (params) =>
  defHttp.post({
    url: Api.CloseFlowProcess,
    params,
  });

export const getHighlightWorkItemApi = (workItemId: number) =>
  defHttp.get({
    url: Api.HighlightWorkItem + '/' + workItemId,
  });

export const getFlowXmlApi = (guid: string) =>
  defHttp.get({
    url: Api.GetFlowXml + '/' + guid + '?v=2',
  });

export const getViewApprovalProcessApi = (workItemId: string) =>
  defHttp.get({
    url: Api.ViewApprovalProcess + '/' + workItemId,
  });

export const getTemplateDefinePageApi = (params?: Object) =>
  defHttp.post<FlowTemplateResult[]>({
    url: Api.TemplateDefinePage,
    params,
  });

// 草稿删除
export const deleteFlowProcessApi = (params, type: ErrorMessageMode = 'msg') =>
  defHttp.post(
    {
      url: Api.DeleteDraft,
      params,
    },
    {
      errorMessageMode: type,
    },
  );

// 发送传阅,意见征询 operationType(0传阅, 1征询)
export const transferFlowApi = (params) =>
  defHttp.post({
    url: Api.TransferFlow,
    params,
  });

// 填写意见
export const transferIdeaFlowApi = (params) =>
  defHttp.post({
    url: Api.TransferIdeaFlow,
    params,
  });

// 转办
export const turnToDoFlowApi = (params) =>
  defHttp.post(
    {
      url: Api.TurnToDoFlow,
      params,
    },
    {
      successCustomMsg: true,
      successMessageMode: 'msg',
    },
  );

// 删除传阅
export const deleteTransferFlowApi = (params, type: ErrorMessageMode) =>
  defHttp.post(
    {
      url: Api.DeleteTransferFlow,
      params,
    },
    {
      errorMessageMode: type,
    },
  );

// 获取修复公文
export const getRestoreWorkItemInfoApi = (params) =>
  defHttp.post({
    url: Api.RestoreWorkItemInfo,
    params,
  });

// 公文步骤
export const getRestoreNewEntryOptionsApi = (params) =>
  defHttp.post({
    url: Api.RestoreNewEntryOptions,
    params,
  });

// 修复公文
export const getRestoreWorkItemApi = (params) =>
  defHttp.post({
    url: Api.RestoreWorkItem,
    params,
  });

// 流程撤回
export const getRecallWorkItemApi = (params) =>
  defHttp.post({
    url: Api.RecallWorkItem,
    params,
  });

// 流程催办
export const getUrgingWorkItemApi = (params) =>
  defHttp.post({
    url: Api.UrgingWorkItem,
    params,
  });

// 流程保存签名
export const saveWorkItemSignInfoApi = (params) =>
  defHttp.post({
    url: Api.saveWorkItemSignInfo,
    params,
  });
// 上传流程签名
export const uploadForWorkItemApi = async (params) => {
  const ret = await defHttp.uploadFile(
    {
      url: Api.uploadForWorkItem,
    },
    params,
  );

  const { data } = ret;
  const hasSuccess = data && Reflect.has(data, 'code') && data.code === ResultEnum.SUCCESS;
  if (hasSuccess && data?.data?.url) {
    return data?.data?.url;
  } else {
    Promise.reject(data);
    return data;
  }
};
// 插件节点最新信息 WatchNodeInfo
export const getWatchNodeInfo = (params: {
  activityName: string;
  activityStatus: number;
  workItemId: number | string;
}) =>
  defHttp.post({
    url: Api.WatchNodeInfo,
    params,
  });
